package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import com.tanhua.server.service.SmallVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Api(tags = "小视频相关接口")
@RestController
@RequestMapping("/smallVideos")
public class SmallVideoController {

    @Autowired
    private SmallVideoService smallVideoService;

    @ApiOperation("视频上传")
    @PostMapping
    public ResponseEntity publish(MultipartFile videoThumbnail, MultipartFile videoFile) {
        smallVideoService.uploadVideo(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("推荐视频列表")
    @GetMapping
    public ResponseEntity getVideoList(@RequestParam(defaultValue = "1") Long page,
                                       @RequestParam(name = "pagesize", defaultValue = "10") Long pageSize) {
        PageResult<VideoVo> pageResult = smallVideoService.getVideoList(page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("视频点赞")
    @PostMapping("/{id}/like")
    public ResponseEntity videoLike(@PathVariable String id) {
        Integer likeCount = smallVideoService.likeVideo(id);
        return ResponseEntity.ok(likeCount);
    }

    @ApiOperation("视频取消点赞")
    @PostMapping("/{id}/dislike")
    public ResponseEntity videoDislike(@PathVariable String id) {
        smallVideoService.dislike(id);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("视频评论列表")
    @GetMapping("/{videoId}/comments")
    public ResponseEntity getComments(@PathVariable String videoId,
                                      @RequestParam(defaultValue = "1") Long page,
                                      @RequestParam(defaultValue = "10", name = "pagesize") Long pageSize) {
        PageResult pageResult = smallVideoService.getComments(videoId, page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("视频评论列表")
    @PostMapping("/{videoId}/comments")
    public ResponseEntity getComments(@PathVariable String videoId, @RequestBody Map<String,String> params) {
        String comment = params.get("comment");
        smallVideoService.publishComments(videoId, comment);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("视频用户关注")
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable Long uid) {
        smallVideoService.userFocus(uid);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("视频用户取消关注")
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable Long uid) {
        smallVideoService.userUnFuces(uid);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("评论点赞")
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentLike(@PathVariable String id) {
        Integer commentLike = smallVideoService.commentLike(id);
        return ResponseEntity.ok(commentLike);
    }

    @ApiOperation("评论取消点赞")
    @PostMapping("/comments/{commentId}/dislike")
    public ResponseEntity commentDislike(@PathVariable String commentId) {
        Integer commentLike = smallVideoService.commentDislike(commentId);
        return ResponseEntity.ok(commentLike);
    }
}
